package com.skyline.wo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.type.Activity;
import com.skyline.base.type.Authority;
import com.skyline.wo.dao.CategoryDao;
import com.skyline.wo.model.Category;
import com.skyline.wo.service.CategoryService;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryDao categoryDao;

	public boolean createCategory(Long ownerId, String ownerPortrait,
			String ownerNickname, String categoryName,
			Activity activity) {
		if (categoryDao.countSameCategory(ownerId, categoryName) == 0) {
			categoryDao.inseartCategory(ownerId, ownerPortrait, ownerNickname,
					categoryName, activity);
			return true;
		}
		else return false;

	}

	public void createDefaultCategory(Long ownerId, String ownerPortrait,
			String ownerNickname) {
		categoryDao.inseartCategory(ownerId, ownerPortrait, ownerNickname,
				 "默认分类", Activity.UNDELETABLE);
	}

	public List<Category> getCategoryByOwnerId(Long ownerId) {

		return categoryDao.queryCategoryByOwnerId(ownerId);

	}
	
	public Category getCategoryById(Long categoryId){
		return categoryDao.queryCategoryById(categoryId);
	}

	public boolean modifyCategory(Long id, String categoryName,
			Long ownerId) {
		if (categoryDao.countSameCategory(ownerId, categoryName) == 0) {
			categoryDao.updateCategory(id,  categoryName, ownerId);
			return true;
		}else{
			return false;
		}
	}

	public void deleteCategory(Long id,Long actioner, Integer authority) {
		if (authority >= Authority.PRIVATE) {
			categoryDao.deleteCategory(id,actioner);
		}
		if (authority.equals(Authority.ADMIN)) {
			// TODO:如果为管理员删除，将该分类下的日志全部删除
		}
		if (authority.equals(Authority.PRIVATE)) {
			// TODO:如果为自己删除分类，将该分类下的日志移到默认分类中
		}
	}

}
